/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.tkorg.coauthorgraph.servlet.graph;

import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import uit.tkorg.coauthorgraph.bo.AuthorBO;
import uit.tkorg.coauthorgraph.bo.CoAuthorGraphBO;
import uit.tkorg.coauthorgraph.graph.CoAuthorGraphEdge;
import uit.tkorg.coauthorgraph.graph.CoAuthorGraphVertex;

/**
 *
 * @author Tin Huynh
 */
public class BuildCoAuthorGraphServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
        try {
            ArrayList initialAuthorIDList = new ArrayList();
            PrintWriter out = response.getWriter();
            String depthLevel = request.getParameter("depthLevel");
            String[] authorList = request.getParameterValues("authorList");
            ArrayList authorNameList = new ArrayList();
            for (int i=0; i<authorList.length; i++) {
                authorNameList.add(authorList[i]);

                AuthorBO authorBO = new AuthorBO();
                int curAuthorID = authorBO.getAuthorID(authorList[i]);
                if (curAuthorID != -1) {
                    initialAuthorIDList.add(new Integer(curAuthorID));
                }
            }
            
            CoAuthorGraphBO coAuthorGraphBO = new CoAuthorGraphBO();
            UndirectedSparseGraph graph = coAuthorGraphBO.buildCoAuthorGraph(authorNameList, depthLevel);

            HttpSession session = request.getSession();
            session.setAttribute("initialAuthorIDList", initialAuthorIDList);
            session.setAttribute("coAuthorGraph", graph);
            System.out.println("************************************************************");
            System.out.println("-------------------DONE DONE DONE --------------------------");
            System.out.println("************************************************************");
            String resultMessage = "Please take a look at the Console to see the building result of the CoAuthorGraph";
            request.setAttribute("resultMessage", resultMessage);
            RequestDispatcher dispatcher = request.getRequestDispatcher("./jsp/MetricsCalculation.jsp");
            dispatcher.forward(request, response);
            //printGraphToConsole(session);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
         * */
         
    }

    /**
     * Print the information of the CoAuthorGraph to Console
     */
    /*
    public void printGraphToConsole(HttpSession session) {
        // Print out the graph
        System.out.println("--------------------------------------------------");
        System.out.println("----------------COAUTHORGRAPH<V,E>-----------------");
        System.out.println("--------------------------------------------------");
        UndirectedSparseGraph graph = (UndirectedSparseGraph) session.getAttribute("coAuthorGraph");
        Collection vertices = graph.getVertices();
        Iterator iter = vertices.iterator();
        CoAuthorGraphVertex curAuthorVertex = null;
        System.out.print("<V> = {...}");
        int index = 0;
        while (iter != null && iter.hasNext()) {
            curAuthorVertex = (CoAuthorGraphVertex) iter.next();
            System.out.println(index + "." + curAuthorVertex.getAuthorName());
            System.out.println("* Conferences:");
            HashMap hm = curAuthorVertex.getConfPaperCount();
            Set set = hm.entrySet();
            Iterator iterator = set.iterator();
            while(iterator.hasNext()){
                Map.Entry me = (Map.Entry)iterator.next();
                System.out.println(me.getKey() + " : " + me.getValue() );
            }

            System.out.println("* Journals:");
            hm = curAuthorVertex.getJourPaperCount();
            set = hm.entrySet();
            iterator = set.iterator();
            while(iterator.hasNext()){
                Map.Entry me = (Map.Entry)iterator.next();
                System.out.println(me.getKey() + " : " + me.getValue() );
            }

            index++;
        }

        System.out.println("<E>:");
        Collection edges = graph.getEdges();
        iter = edges.iterator();
        CoAuthorGraphEdge curAuthorEdge = null;
        StringBuffer edgeStr = new StringBuffer();
        while (iter != null && iter.hasNext()) {
            curAuthorEdge = (CoAuthorGraphEdge) iter.next();
            edgeStr.append("[");
            edgeStr.append(curAuthorEdge.getFirstAuthorName() + ", " + curAuthorEdge.getSecondAuthorName());
            edgeStr.append("]; ");
        }
        System.out.println(edgeStr.toString());

    }
     * *
     */
}
